package com.chima.service.impl;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import org.hibernate.criterion.DetachedCriteria;
import com.chima.dao.EmployeeDao;
import com.chima.dao.RateDao;
import com.chima.domain.Employees;
import com.chima.domain.MiddleEmpRate;
import com.chima.domain.PageRequestBean;
import com.chima.domain.PageResponseBean;
import com.chima.domain.Rate;
import com.chima.service.RateService;

public class RateServiceImpl implements RateService {
	private RateDao  rateDao;
	private EmployeeDao  employeeDao;
	
	public RateDao getRateDao() {
		return rateDao;
	}

	public EmployeeDao getEmployeeDao() {
		return employeeDao;
	}

	public void setEmployeeDao(EmployeeDao employeeDao) {
		this.employeeDao = employeeDao;
	}

	public void setRateDao(RateDao rateDao) {
		this.rateDao = rateDao;
	}

	public PageResponseBean findRate(PageRequestBean pageRequestBean) {
		int firstResult =(pageRequestBean.getPage()-1)*pageRequestBean.getLimit();
		int maxResults =pageRequestBean.getLimit();
		int total = rateDao.findByTotalCount(pageRequestBean.getDate(),pageRequestBean.getDept());
		// 获取分页查询条件
//		DetachedCriteria detachedCriteria = DetachedCriteria.forClass(Rate.class);
		DetachedCriteria detachedCriteria = pageRequestBean.getCriteria();// 不一定存在
		List<Rate> rates = rateDao.findByCriteria(detachedCriteria, firstResult, maxResults);
		PageResponseBean pageResponseBean = new PageResponseBean();
		pageResponseBean.setRows(rates);
		pageResponseBean.setTotal(total);
		return pageResponseBean;
	}

	public void updateGrade(Double grade, int id) {
	
		rateDao.updateGrade(grade, id);
	}

	public Rate findRateById(int id) {
		
		return rateDao.findById(id);
	}

	public List<Rate> findByDate(String date) {
		return rateDao.findByDate(date);
	}

	public void save(Rate rate) {
		rateDao.save(rate);
	}

	/**
	 * 定时插入评级
	 */
	public void insertRate() {
		Date d= new Date();
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM");
		String date = formatter.format(d);
		List<Rate> rates=rateDao.findByDate(date);
		if(rates.size()==0){
			System.out.println("jinru.................");
			List<Employees> employees= employeeDao.findALL();
				for(Employees e:employees ){
					if(e.getDepartment().getId()!=36){
						if (e.getDepartment().getXzorzy()==1) {
							Rate r=new Rate();
							r.setDate(date);
							r.setDepartment(e.getDepartment());
							r.setGrade(100.0);
							r.setEmployee(e);
							r.setKslx(1);
							if (e.getPosition()==10) {
							r.setFlagzuzhang(1)	;
							}
							
							r.setZuname(e.getZuname());
							r.setPosition(e.getPosition());
							r.setActualworkday(getdays());
							r.setWorkday(getdays());
							rateDao.save(r);
						}else {
							Rate r=new Rate();
							r.setDate(date);
							r.setKslx(0);
							r.setZuname("不需要");
							r.setFlagzuzhang(0);
							r.setDepartment(e.getDepartment());
							r.setGrade(100.0);
							r.setEmployee(e);
							r.setPosition(e.getPosition());
							r.setActualworkday(getdays());
							r.setWorkday(getdays());
							rateDao.save(r);
						}
						
					}
				}
				System.out.println("wancheng.................");
			
		}
	}

	public List<Rate> findByDateaAndDepart(String serchdate,int searchdepartment) {
		
		return rateDao.findByDateAndDepart(serchdate, searchdepartment);
	}

	public void savaOrupdateRate(String serchdate, Employees e) {
		Rate r=rateDao.findByDateAndEmp(serchdate, e);
		if(r==null){
			Rate rate =new Rate();
			rate.setDate(serchdate);
			rate.setDepartment(e.getDepartment());
			rate.setEmployee(e);
			rate.setGrade(1.3);
			rate.setPosition(e.getPosition());
			rate.setWorkday(getDayByYearAndMonth(serchdate));
			rateDao.save(rate);
		}
		
	}

	public List<Rate> findByDepart(int searchdepartment) {
		
		return rateDao.findByDepart(searchdepartment);
	}

	public PageResponseBean findMiddleRate(PageRequestBean pageRequestBean) {
		
		int firstResult =(pageRequestBean.getPage()-1)*pageRequestBean.getLimit();
		int maxResults =pageRequestBean.getLimit();
		// 获取分页查询条件
//		DetachedCriteria detachedCriteria = DetachedCriteria.forClass(Rate.class);
		DetachedCriteria detachedCriteria = pageRequestBean.getCriteria();// 不一定存在
		List<Rate> middleEmpRates = rateDao.findMiddleEmpRateByCriteria(detachedCriteria, firstResult, maxResults);
		int total = rateDao.findMiddleTotalCount(pageRequestBean.getDate());
		PageResponseBean pageResponseBean = new PageResponseBean();
		pageResponseBean.setRows(middleEmpRates);
		pageResponseBean.setTotal(total);
		return pageResponseBean;
	}

	public void updateMiddleGrade(Double grade, int id) {
		
		rateDao.updateMiddleGrade(grade, id);
	}

	public List<Rate> findMiddleEmpRateByDate(String date,int position) {
		
		return rateDao.findMiddleEmpRateByDate(date,position);
	}

	public List<MiddleEmpRate> findMiddleEmpRateByDateAndDept(String date,String dept) {
	
		return rateDao.findMiddleEmpRateByDateAndDept(date, dept);
	}

	public void saveMiddleEmpRate(MiddleEmpRate m) {
		
		rateDao.saveMiddleEmpRate(m);
	}

	public void savaOrupdateMiddleRate(String serchdate, Employees e) {
		
		MiddleEmpRate rate=rateDao.findByEmpNameAnddate(e.getName(), serchdate);
		if(rate==null){
			MiddleEmpRate m =new MiddleEmpRate();
		    m.setDate(serchdate);
		    m.setDepartment(e.getDepartment().getName());
		    m.setEmployee(e.getName());
		    m.setGrade(1.3);
		    m.setDeptId(String.valueOf(e.getDepartment().getId()));
			rateDao.saveMiddleEmpRate(m);
		}
	}

	public Rate findByEmpAndDate(Employees emp, String date) {
		
		return rateDao.findByDateAndEmp(date, emp);
	}

	public void updateWorkDay(int workday, int id) {
		
		 rateDao.updateWorkDay(workday, id);
		 
	}

	public void deleterate(int empid) {
		
		rateDao.deleterate(empid);
	}

/**
 * 根据日期获得天数
 */
	 private static int getDayByYearAndMonth(String date){
			String dyear=date.substring(0, 4);
			String dmouth=date.substring(5, 7);
		     SimpleDateFormat simpleDate = new SimpleDateFormat("yyyy/MM");
		    Calendar rightNow = Calendar.getInstance();
		    try{
		    rightNow.setTime(simpleDate.parse(dyear+"/"+dmouth));
		    }catch(ParseException e){
		    e.printStackTrace();
		    }
		    return rightNow.getActualMaximum(Calendar.DAY_OF_MONTH);
	}
		 
 /**
  * 
  *获得本月天数
  */
 	 private static int getdays(){
 		Calendar cal = Calendar.getInstance();
 		cal.set(Calendar.MONTH,cal.get(Calendar.MONTH)+1);
 		cal.set(Calendar.DAY_OF_MONTH,1);
 		cal.set(Calendar.DATE,cal.get(Calendar.DATE)-1);
 		int days=cal.get(Calendar.DAY_OF_MONTH);
 		System.out.println("当月天数:"+cal.get(Calendar.DAY_OF_MONTH));
 		return days;
 	}

}
